<?php $this->context->layout = 'form';?>
<?= \common\widgets\Asset::widget(['type'=>['tagsinput','summernote']])?>
<style>
    #container {
        width: 100%;
        height: 1000px;
    }
</style>
<form class="form-horizontal m" id="form-config-add">
    <div class="layui-tab layui-tab-brief" lay-filter="test1">
        <ul class="layui-tab-title" style="text-align: center">
            <li class="layui-this" data-formname="" lay-id="0">
                信息
            </li>
            <li class="" data-formname="" lay-id="1">
                详情
            </li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show" data-id="0">
                <input type="hidden" name="id" value="<?=$info['id']?>">
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|名称','extend'=>['name'=>'name','required'=>1,'info'=>$info]])?>
                <div class="form-group mb0">
                    <label class="col-sm-3 control-label">分类：</label>
                    <div class="col-sm-3 mb15">
                        <?= \common\widgets\FormInput::widget(['name'=>'select','extend'=>['name'=>'c_id','data'=>$this->params['classify'],'class'=>'form-control','info'=>$info]])?>
                    </div>
                </div>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|价格','extend'=>['name'=>'price','type'=>'number','required'=>1,'info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|总数','extend'=>['name'=>'total','type'=>'number','required'=>1,'info'=>$info]])?>
                <div class="form-group mb0">
                    <label class="col-sm-3 control-label">主图：</label>
                    <div class="col-sm-2 mb15">
                        <?= \common\widgets\FormInput::widget(['name'=>'image','extend'=>['link'=>'false','name'=>'imgarr1','id'=>'uploadimgbtn1','script_after'=>'1','cat'=>'bmimage','water'=>'true','sm'=>2,'data'=>$info['main_img']]])?>
                    </div>
                    <label class="col-sm-1 control-label">图片：</label>
                    <div class="col-sm-2 mb15">
                        <?= \common\widgets\FormInput::widget(['name'=>'image','extend'=>['link'=>'false','name'=>'imgarr2','id'=>'uploadimgbtn2','script_after'=>'2','cat'=>'bmimage','water'=>'true','sm'=>3,'data'=>$info['img']]])?>
                    </div>
                </div>
                <input type="hidden" name="main_img" id="img1" value="">
                <input type="hidden" name="img" id="img2" value="">
                <input type="hidden" name="recommend_img" id="img3" value="">
                <div class="form-group mb0">
                    <label class="col-sm-3 control-label is-required">活动时间：</label>
                    <div class="col-sm-5 mb15">
                        <input type="text" id="start" name="times" autocomplete="off" class="form-control starttime" required value="<?=date('Y-m-d',strtotime($info['start_time'])).' ~ '.date('Y-m-d',strtotime($info['end_time']))?>">
                    </div>
                </div>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|手机号','extend'=>['name'=>'mobile','required'=>1,'info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'formradio|状态','extend'=>['name'=>'status','required'=>1,'info'=>$info]])?>

                <div class="form-group mb0">
                    <label class="col-sm-3 control-label">首页推荐：</label>
                    <div class="col-sm-2 mb15">
                        <?= \common\widgets\FormInput::widget(['name'=>'radio','extend'=>['name'=>'is_recommend','required'=>1,'info'=>$info,'data'=>['否','是']]])?>
                    </div>
                    <label class="col-sm-1 control-label">首页推荐图：</label>
                    <div class="col-sm-2 mb15">
                        <?= \common\widgets\FormInput::widget(['name'=>'image','extend'=>['link'=>'false','name'=>'imgarr3','id'=>'uploadimgbtn3','script_after'=>'3','cat'=>'bmimage','water'=>'true','sm'=>3,'data'=>$info['recommend_img']]])?>
                    </div>
                </div>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|排序','extend'=>['name'=>'sort','required'=>1,'value'=>0,'type'=>'number','值越大越靠前','info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|地址','extend'=>['name'=>'address','id' => 'tipinput',  'tips' => '请输入完整地址，便于定位，下方地图可以点击选择','required'=>1,'info'=>$info]])?>
                <input type="hidden" name="province" value="">
                <input type="hidden" name="city" value="">
                <input type="hidden" name="county" value="">
                <input type="hidden" name="street" value="">
                <input type="hidden" name="address" value="">
                <input type="hidden" name="long" value="">
                <input type="hidden" name="lat" value="">
                <div class="form-group mb0"><div id="addr" style="font-size: large;margin: 10px 62px;"></div></div>
                <?= $this->render('/iframe',['name'=>'tabfooter'])?>
                <div id="container"></div>
            </div>
            <div class="layui-tab-item" data-id="1">
                <div class="form-group">
                    <label class="col-sm-2 control-label">商品详情：</label>
                    <div class="col-sm-9">
                        <?= \common\widgets\FormInput::widget(['name' => 'textarea', 'extend' => ['name' => 'details', 'class' => 'summernote_content hide', 'info' => $info]]) ?>
                        <div class="summernote" data-place="" data-height="600px"></div>
                    </div>
                </div>
                <?= $this->render('/iframe',['name'=>'tabfooter'])?>
            </div>
        </div>
    </div>

</form>

<?php $this->beginBlock('script'); ?>
<script type="text/javascript">
    window._AMapSecurityConfig = {
        serviceHost: '<?=Yii::$app->request->hostInfo?>/_AMapService',
    }
</script>
<script type="text/javascript"
        src="//webapi.amap.com/maps?v=1.4.8&key=2a2736912822fc1e6f6fc00ec40f5596&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script>
<script type="text/javascript" src="//cache.amap.com/lbs/static/addToolbar.js"></script>
<script>
    function submitHandler() {
        if ($.validate.form()) {
            imgassign("input[name='imgarr1[]']",'#img1')
            imgassign("input[name='imgarr2[]']",'#img2')
            imgassign("input[name='imgarr3[]']",'#img3')
            var sHTML = $('.summernote').summernote('code');
            $(".summernote_content").val(sHTML);
            $.operate.save("<?= \yii\helpers\Url::toRoute('list-edit')?>", $('#form-config-add').serialize());
        }
    }
    layui.use(['laydate','element'], function () {
        var elementtt = layui.element;
        var laydate = layui.laydate;
        laydate.render({
            elem: "#start" //指定元素
            , range: '~'
            , trigger: 'click' //采用click弹出
            , ready: function (value, data) {

            }
        })
    });
    var marker = null;
    var map = null;
    window.onload = function () {
        //定义初始经纬度
        var mylgt = "<?=$info['long']?>";
        var mylat = "<?=$info['lat']?>";
        //地图加载
        map = new AMap.Map("container", {
            zoom: 15,    //地图缩放级别
            resizeEnable: true,
        });
        markerDi(mylgt,mylat)
        //为地图注册click事件获取鼠标点击出的经纬度坐标
        var clickEventListener = map.on('click', function (e) {
            console.log(e)
            markerDi(e.lnglat.lng, e.lnglat.lat)

        });
        //输入地点提示
        var auto = new AMap.Autocomplete({
            input: "tipinput"
        });
        //构造地点查询类
        var placeSearch = new AMap.PlaceSearch({
            map: map
        });
        //注册监听，当选中某条记录时会触发
        AMap.event.addListener(auto, "select", select);

        function select(e) {
            console.log(e)
            placeSearch.setCity(e.poi.adcode);
            placeSearch.search(e.poi.name);  //关键字查询查询
            markerDi(e.poi.location.lng, e.poi.location.lat)
        }
    }

    function markerDi(lng, lat) {

        if (marker) {
            marker.setMap(null);
            marker = null;
        }
        //重设标记
        marker = new AMap.Marker({
            icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png",
            position: [lng, lat],
            extData: {
                "markerLng": lng,
                "markerLat": lat,
            },
        });
        map.setZoomAndCenter(14, [lng, lat]);
        marker.setMap(map);
        getPositionByLonLats(lng, lat)
    }

    function getPositionByLonLats(lng, lat) {
        var lnglatXY = [lng, lat];// 地图上所标点的坐标
        AMap.service('AMap.Geocoder', function () {// 回调函数
            geocoder = new AMap.Geocoder({});
            geocoder.getAddress(lnglatXY, function (status, result) {
                if (status=='complete'&&result.info=='OK'){
                    var province = result.regeocode.addressComponent.province;
                    var city = result.regeocode.addressComponent.city;
                    var county = result.regeocode.addressComponent.district;
                    var street = result.regeocode.addressComponent.township;
                    var address = result.regeocode.addressComponent.street+result.regeocode.addressComponent.streetNumber;
                    $('input[name=province]').val(province)
                    $('input[name=city]').val(city)
                    $('input[name=county]').val(county)
                    $('input[name=street]').val(street)
                    $('input[name=address]').val(province+city+county+street+address)
                    $('input[name=long]').val(lng)
                    $('input[name=lat]').val(lat)
                    $('#addr').html("解析地址："+province+' '+city+' '+county+' '+street+' '+address)
                }else {
                    alert('经纬度解析失败')
                }
            });
        });
    }
</script>
<?php $this->endBlock(); ?>
